package cn.zjyj.module.zjyj.service.riskassessmentinstancerel;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.zjyj.module.zjyj.controller.admin.riskassessmentinstancerel.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.riskassessmentinstancerel.RiskAssessmentInstanceRelDO;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;

import cn.zjyj.module.zjyj.dal.mysql.riskassessmentinstancerel.RiskAssessmentInstanceRelMapper;

import static cn.zjyj.framework.common.common.exception.util.ServiceExceptionUtil.exception;
import static cn.zjyj.framework.common.common.util.collection.CollectionUtils.convertList;
import static cn.zjyj.module.zjyj.enums.ErrorCodeConstants.*;

/**
 * 风险评估实例关系 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class RiskAssessmentInstanceRelServiceImpl implements RiskAssessmentInstanceRelService {

    @Resource
    private RiskAssessmentInstanceRelMapper riskAssessmentInstanceRelMapper;

    @Override
    public Long createRiskAssessmentInstanceRel(RiskAssessmentInstanceRelSaveReqVO createReqVO) {
        // 插入
        RiskAssessmentInstanceRelDO riskAssessmentInstanceRel = BeanUtils.toBean(createReqVO, RiskAssessmentInstanceRelDO.class);
        riskAssessmentInstanceRelMapper.insert(riskAssessmentInstanceRel);
        // 返回
        return riskAssessmentInstanceRel.getId();
    }

    @Override
    public void updateRiskAssessmentInstanceRel(RiskAssessmentInstanceRelSaveReqVO updateReqVO) {
        // 校验存在
        validateRiskAssessmentInstanceRelExists(updateReqVO.getId());
        // 更新
        RiskAssessmentInstanceRelDO updateObj = BeanUtils.toBean(updateReqVO, RiskAssessmentInstanceRelDO.class);
        riskAssessmentInstanceRelMapper.updateById(updateObj);
    }

    @Override
    public void deleteRiskAssessmentInstanceRel(Long id) {
        // 校验存在
        validateRiskAssessmentInstanceRelExists(id);
        // 删除
        riskAssessmentInstanceRelMapper.deleteById(id);
    }

    @Override
        public void deleteRiskAssessmentInstanceRelListByIds(List<Long> ids) {
        // 校验存在
        validateRiskAssessmentInstanceRelExists(ids);
        // 删除
        riskAssessmentInstanceRelMapper.deleteByIds(ids);
        }

    private void validateRiskAssessmentInstanceRelExists(List<Long> ids) {
        List<RiskAssessmentInstanceRelDO> list = riskAssessmentInstanceRelMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RISK_ASSESSMENT_INSTANCE_REL_NOT_EXISTS);
        }
    }

    private void validateRiskAssessmentInstanceRelExists(Long id) {
        if (riskAssessmentInstanceRelMapper.selectById(id) == null) {
            throw exception(RISK_ASSESSMENT_INSTANCE_REL_NOT_EXISTS);
        }
    }

    @Override
    public RiskAssessmentInstanceRelDO getRiskAssessmentInstanceRel(Long id) {
        return riskAssessmentInstanceRelMapper.selectById(id);
    }

    @Override
    public PageResult<RiskAssessmentInstanceRelDO> getRiskAssessmentInstanceRelPage(RiskAssessmentInstanceRelPageReqVO pageReqVO) {
        return riskAssessmentInstanceRelMapper.selectPage(pageReqVO);
    }

}